<template>
	<!-- 分类 -->
	<view class="cate">
		<view class="title">导航</view>
		<view class="cate-list">
			<view v-for="(item,index) in cates" @click="chooseCateFn(item.name, index)" :key="item.id"
				:class="['opt',num == index ? 'active':'']" style="text-align: center;">
				<image :src="item.iconUrl" mode="widthFix" style="width: 40rpx;height:40rpx"></image>
				<view>{{item.name}}</view>
			</view>

			<!-- 骨架结构 -->
			<view class="cate-list-body" v-if="isLoadingCate">
				<view class="body-opt" v-for="(item,i) in 4" :key="i"></view>
			</view>

		</view>
		<view class="title">菜单</view>
		<view class="menu-list" v-if="menus.length != 0">
			<view v-for="(item,index) in menus" :key="item.id" class="item">
				<navigator :url="`/pages/topiclist/topiclist?text=${item.text}`">
					<view><text class="circle">{{index + 1}}</text></view>
					<view class="text">
						{{item.text}}
					</view>
				</navigator>
			</view>
		</view>

		<view v-if="menus.length == 0" class="message">暂无子菜单~~</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';

	const num = ref(0)

	// 导航数组
	const cates = ref([])
	// 子菜单数组
	const menus = ref([])

	// 控制骨架显示隐藏
	const isLoadingCate = ref(true)

	const chooseCateFn = (name, value) => {
		num.value = value;
		// 用户点击切换的导航对应的子菜单
		getMenuFn(name)
	}

	// 编写函数获取子分类数据
	const getMenuFn = async (name) => {
		await uniCloud.callFunction({
				name: 'menu',
				data: {
					name: name
				}
			})
			.then(
				resData => {
					console.log(resData)
					// 获取子分类列表
					if (resData.result && resData.result.data) {
						menus.value = resData.result.data;
					}
				}
			)
			.catch(err => {
				console.log(err)
			})
	}

	// 编写函数获取导航数据
	const getCategoryFn = async () => {
		await uniCloud.callFunction({
				name: 'category'
			})
			.then(
				resData => {
					console.log(resData)
					// 获取导航分类列表
					if (resData.result && resData.result.data) {
						let data = resData.result.data;
						cates.value = data;
						// 加载完成
						isLoadingCate.value = false;
						// 直接调用方法获取子分类
						if (data.length > 0) {
							getMenuFn(data[0].name);
						}

					}
				}
			)
			.catch(err => {
				console.log(err)
			})
	}

	// 初始化
	getCategoryFn();
</script>

<style lang="scss">
	@import "../../static/scss/variable.scss";

	.cate {
		padding: $padd;
	}

	.cate-list-body {
		width: 100%;
		display: flex;
		justify-content: space-between;

		.body-opt {
			width: 148rpx;
			height: 126rpx;
			background-color: #f0f0f0;
			margin-right: 10rpx;
			margin-top: 10rpx;
		}
	}

	.cate-list {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;

		.opt {
			padding: 12rpx 40rpx;
			border: 1px solid #f0f0f0;
			margin-right: 10rpx;
			margin-top: 10rpx;
			font-size: 32rpx;
			flex-shrink: 0;

			&.active {
				// background-color: $theme-color;
				color: $theme-color;
				box-shadow: 2px 2px 2px #f0f0f0;
			}
		}


	}

	.menu-list {
		width: 100%;
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		grid-auto-rows: auto;
		row-gap: 20rpx;
		column-gap: 20rpx;
		box-sizing: border-box;
		margin-top: 20rpx;

		.item {
			padding: 20rpx 20rpx;
			flex-shrink: 0;
			border: 1px solid #f0f0f0;
			box-sizing: border-box;
			text-align: center;

			.circle {
				display: inline-block;
				width: 70rpx;
				height: 70rpx;
				border: 1px solid $theme-color;
				border-radius: 50%;
				text-align: center;
				line-height: 70rpx;
				color: $theme-color;
				margin-bottom: 16rpx;
			}

			.text {
				font-weight: bold;
			}
		}
	}
</style>